home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / text / tex / texclean.lha / texclean.c < prev    next >
C/C++ Source or Header  |  1993-03-28  |  3KB  |  163 lines

  1. /*****************************************************************************
  2.  * TeXClean:    Delete files generated by TeX, LaTeX, and BibTeX.
  3.  *        Does not affect ".tex" files.
  4.  *
  5.  * Author:    Daniel J. Barrett.  Originally a UNIX shell script.
  6.  *
  7.  * Updates:
  8.  *
  9.  *    1.00    Original version.
  10.  *    1.10    Now deletes texput.log and mfput.log.
  11.  *        Added version string.
  12.  *    1.11    Support for AmigaDOS "?" character to print a Usage
  13.  *        message.
  14.  *        Put dots in front of the suffix names in the Usage message.
  15.  *
  16.  * Someday...
  17.  *
  18.  *    Add wildcard support.
  19.  *    But do it so the program is portable!
  20.  *
  21.  ****************************************************************************/
  22.  
  23. char *version = "$VER: TeXClean 1.11 23/11/92";
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <exec/types.h>
  28.  
  29.  
  30. #define    SUFFIX_DELIMITER    '.'
  31. #define    EXISTS(filename)    !access(filename, 0)
  32. #define    EQUAL(str1, str2)    !strcmp(str1, str2)
  33.  
  34.  
  35. /* NEVER NEVER NEVER put "tex" in this ending list.
  36.  * Always end with NULL. */
  37.  
  38. char *endList[] = { "aux", "bbl", "blg", "dvi", "idx", "lof", "log", "lot", 
  39.             "toc", NULL};
  40.  
  41. /* Other junk files to be deleted if they exist. */
  42.  
  43. char *dieList[] = { "ShowDVI.log", "texput.log", "mfput.log", NULL};
  44.  
  45.     
  46. char *RemoveSuffix(char *filename);
  47. BOOL SanityCheck(char *endList[]);
  48. long DeleteMe(char *filename);
  49. void TeXClean(char *filename, char *endings[]);
  50. void Usage(char *prog, char *endings[], char *crap[]);
  51.  
  52.     
  53. main(int argc, char *argv[])
  54. {
  55.     char **s;
  56.  
  57.     if (!SanityCheck(endList))
  58.     {
  59.         fprintf(stderr, "INTERNAL ERROR IN ENDLIST!!!!!!\n");
  60.         exit(20);
  61.     }
  62.  
  63.     if ((argc <= 1) || (EQUAL(argv[1], "?")))
  64.         Usage(argv[0], endList, dieList);
  65.     else
  66.     {
  67.         while (*(++argv))
  68.         {
  69.             TeXClean(RemoveSuffix(*argv), endList);
  70.         }
  71.  
  72.         s = dieList;
  73.         while (*s)
  74.             (void) DeleteMe(*(s++));
  75.     }
  76.  
  77.     exit(0);
  78. }
  79.  
  80.     
  81. /* Make sure "tex" is not in the ending list. */
  82. BOOL SanityCheck(char *endList[])
  83. {
  84.     while (*endList)
  85.     {
  86.         if (EQUAL(*endList, "tex"))
  87.             return(FALSE);
  88.         endList++;
  89.     }
  90.     return(TRUE);
  91. }
  92.  
  93.  
  94. /* Print a usage message. */
  95. void Usage(char *prog, char *endings[], char *crap[])
  96. {
  97.     fprintf(stderr, "%s removes TeX files with these endings:\n", prog);
  98.     while (*endings)
  99.         fprintf(stderr, " .%s", *(endings++));
  100.  
  101.     fprintf(stderr, "\nThe following files are also deleted if they"
  102.             " exist:\n");
  103.     while (*crap)
  104.         fprintf(stderr, "\t%s\n", *(crap++));
  105.  
  106.     fprintf(stderr, "\nFiles ending with \".tex\" CANNOT be removed.\n");
  107.     fprintf(stderr, "Usage: %s texfile1 texfile2 ...\n", prog);
  108. }
  109.  
  110.  
  111. /* Remove the stuff after and including the last SUFFIX_DELIMITER. */
  112. char *RemoveSuffix(char *filename)
  113. {
  114.     char *here;
  115.  
  116.     here = rindex(filename, SUFFIX_DELIMITER);
  117.     if (here)
  118.         *here = '\0';
  119.     return(filename);
  120. }
  121.  
  122.  
  123. /* Delete all files with the given prefix and a suffix in endings. */
  124.  
  125. void TeXClean(char *prefix, char *endings[])
  126. {
  127.     char filename[BUFSIZ];
  128.  
  129.     while (*endings)
  130.     {
  131.         (void) sprintf(filename, "%s.%s", prefix, *(endings++));
  132.         (void) DeleteMe(filename);
  133.     }
  134. }
  135.  
  136.  
  137. long DeleteMe(char *filename)
  138. {
  139.     long err;
  140.  
  141.     if (EXISTS(filename))
  142.         {
  143.  
  144. #ifdef DEBUG
  145.         fprintf(stderr, "Pretend to delete \"%s\"\n", filename);
  146.         if (0)    /* Never execute. */
  147. #else
  148.         if (!DeleteFile(filename))
  149. #endif
  150.         {
  151.             err = IoErr();
  152.             fprintf(stderr, "ERROR: can't delete %s\n",
  153.                 filename);
  154.             fprintf(stderr,
  155.                 "Type \"fault %ld\" for more info.\n",
  156.                     err);
  157.         }
  158.         else
  159.             printf("%s -- deleted\n", filename);
  160.     }
  161.     return(err);
  162. }
  163.